package com.dragon.mmochat.message;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatFamilyType;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiModuleMessage;
import com.skymobi.message.SkymobiProtocolMessage.UserConnInfo;

public class Mmochat_EnterOtherServerMap extends SkymobiModuleMessage {
	private long skyId;
	private int roleId;
	private int srcInstId = 0; // 当前服的实例号
	private int srcServerId; // 当前服的ID
	private UserConnInfo conn;
	private String name;
	private int level;
	private MmochatFamilyType family;

	public Mmochat_EnterOtherServerMap() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_C_1_28);
	}

	public Mmochat_EnterOtherServerMap(int dstId) {
		setDstId(dstId); // 另一个服务器的实例号
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_C_1_28);
	}

	@Override
	protected void decodeBody(SkymobiBuffer in) {
		srcInstId = in.getInt();
		srcServerId = in.getInt();
		skyId = in.getLong();
		roleId = in.getInt();
		name = in.getPrefixedString(1);
		level = in.getInt();
		family = MmochatFamilyType.get(in.getInt());

		try {
			int len = in.getInt();
			byte[] bb = new byte[len];
			in.get(bb);
			ByteArrayInputStream in1 = new ByteArrayInputStream(bb);
			ObjectInputStream oi = new ObjectInputStream(in1);
			conn = (UserConnInfo) oi.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		out.putInt(srcInstId);
		out.putInt(srcServerId);
		out.putLong(skyId);
		out.putInt(roleId);
		out.putPrefixedString(name, 1);
		out.putInt(level);
		out.putInt(family.getValue());

		// UserConnInfo
		try {
			ByteArrayOutputStream out1 = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(out1);
			oo.writeObject(conn);
			byte[] bb = out1.toByteArray();
			out.putInt(bb.length);
			out.put(bb);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public int getSrcInstId() {
		return srcInstId;
	}

	public void setSrcInstId(int srcInstId) {
		this.srcInstId = srcInstId;
	}

	public int getSrcServerId() {
		return srcServerId;
	}

	public void setSrcServerId(int srcServerId) {
		this.srcServerId = srcServerId;
	}

	public long getSkyId() {
		return skyId;
	}

	public void setSkyId(long skyId) {
		this.skyId = skyId;
	}

	public UserConnInfo getConn() {
		return conn;
	}

	public void setConn(UserConnInfo conn) {
		this.conn = conn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public MmochatFamilyType getFamily() {
		return family;
	}

	public void setFamily(MmochatFamilyType family) {
		this.family = family;
	}

}
